library(readr)
library(readxl)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(rmarkdown)
library(markdown)
library(ggmap)
## Loading required package: ggplot2
library(Rmisc)
## Loading required package: lattice
## Loading required package: plyr
## -------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## -------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
# Define function for reading in xls for phase 1 data
phase_1_read_data <- function(file_name = 'Form A core.xlsx'){
  message(paste0('Reading and cleaning ', file_name))
  # Read in the data
  if(grepl('.xlsx', file_name)){
    temp <- read_excel(paste0('data/', file_name))
  } else {
    if(file_name == 'Form D directed.csv'){
      # fix weird issue with character being converted to numeric
      temp <- read_csv(paste0('data/', file_name))
      class_column <- read.csv(paste0('data/', file_name))
      temp$CLASS <- as.character(class_column$CLASS)
      rm(class_column)
    } else {
      temp <- read_csv(paste0('data/', file_name))
    }
  }
  # Remove empty rows
  bad_rows <- rep(FALSE, nrow(temp))
  for (row in 1:nrow(temp)){
    empty <- c()
    for (column in 1:ncol(temp)){
      empty[column] <- is.na(temp[row, column])
    }
    if(all(empty)){
      bad_rows[row] <- TRUE
    }
  }
  temp$bad_row <- bad_rows
  temp <- temp[!temp$bad_row,]
  temp$bad_row <- NULL
  # Assign to an appropriate name
  object_name <- 
    gsub(' ', '_', gsub('.xlsx|.csv', '', tolower(file_name)))
  assign(object_name,
         temp,
         envir = .GlobalEnv)
}

# data_files <- dir('data')
data_files <-  "Form A core.csv"
for (i in 1:length(data_files)){
  phase_1_read_data(data_files[i])
}
## Reading and cleaning Form A core.csv
# Narrow down form A core: school
form_a_core <-
  form_a_core %>%
  dplyr::select(SCHOOL_UUID,
                GPS_ALT,
                GPS_ACC,
                DISTRICT,
                TYPE_OF_LATRINE,
                SCHOOL_CONDITIONS,
                SCHOOL_NAME,
                # `valid schools`,
                GPS_LAT,
                GPS_LNG,
                LUNCH,
                NUMBER_OF_CLASSES)
# Read in the valid schools info
# to standardize school names
valid_schools <- read_excel('misc/School names.xlsx')

# Fix names
form_a_core <-
  form_a_core %>%
  left_join(valid_schools %>%
              dplyr::select(`School names…`,
                            `Correct name`),
            by = c('SCHOOL_NAME' = 'School names…')) %>%
  mutate(SCHOOL_NAME = `Correct name`) %>%
  dplyr::select(-`Correct name`)

# Save
school_locations <- 
  form_a_core %>%
  dplyr::select(SCHOOL_NAME,
                GPS_LAT,
                GPS_LNG) %>%
  dplyr::rename(lng = GPS_LNG,
         lat = GPS_LAT) %>%
  filter(!is.na(SCHOOL_NAME)) %>%
  arrange(SCHOOL_NAME) %>%
  filter(!duplicated(SCHOOL_NAME)) %>%
  filter(!is.na(lng))

# Print table
knitr::kable(school_locations)
SCHOOL_NAME lat lng
EPC 3 de Fevereiro -25.15789 32.79680
EPC 7 de Abril -25.38794 32.80348
EPC Aguiar -25.05964 32.76103
EPC Bobi -25.07279 32.72785
EPC Chicuachane -25.16651 32.81351
EPC Duco -24.92529 32.54166
EPC Eduardo Mondlane -25.44020 32.77714
EPC Graca Machel -25.02683 32.64579
EPC Ilha Josinha Machel -25.09652 32.92823
EPC Maciana -25.43374 32.77943
EPC Maguiguana -25.03817 32.66983
EPC Maragra -25.45281 32.77804
EPC Mawandla -25.03316 32.63385
EPC Mepambe -25.05376 32.78838
EPC Moine -24.89020 32.53051
EPC Motaze -24.79970 32.85639
EPC Mulembja -25.40387 32.79537
EPC Nhiwane -24.86376 32.30560
EPC Nwambyana -24.81660 32.89678
EPC Palmeira -25.26101 32.86969
EPC Panjane -24.89846 32.35002
EPC Simbe -24.81854 32.51596
EPC Xinavane -25.04411 32.79003
# Define function for getting school locations
map <- function(i){
  x = school_locations[i,]
  y1 <- ggmap::get_googlemap(center = c(lon = x$lng,
                                  lat = x$lat),
                            zoom = 15)
    y2 <- ggmap::get_googlemap(center = c(lon = x$lng,
                                  lat = x$lat),
                            zoom = 15,
                            maptype = 'satellite')
      y3 <- ggmap::get_googlemap(center = c(lon = x$lng,
                                  lat = x$lat),
                                 maptype = 'satellite',
                            zoom = 17)
        y4 <- ggmap::get_googlemap(center = c(lon = x$lng,
                                  lat = x$lat),
                            zoom = 19,
                            maptype = 'satellite')
  
  z1 <- 
    ggmap(y1) +
    ggtitle(school_locations$SCHOOL_NAME[i],
            paste0(x$lng, ' ', x$lat)) +
    geom_point(data = x,
               aes(x = lng,
                   y = lat),
               color = 'red') 
  z2 <- ggmap(y2) +
    ggtitle(school_locations$SCHOOL_NAME[i]) +
    geom_point(data = x,
               aes(x = lng,
                   y = lat),
               color = 'red')
    z3 <- ggmap(y3) +
    ggtitle(school_locations$SCHOOL_NAME[i]) +
    geom_point(data = x,
               aes(x = lng,
                   y = lat),
               color = 'red')
      z4 <- ggmap(y4) +
    ggtitle(school_locations$SCHOOL_NAME[i]) +
    geom_point(data = x,
               aes(x = lng,
                   y = lat),
               color = 'red')
      
      # print(z1);
      # print(z2);
      # print(z3);
      # print(z4)
      multiplot(z1, z2, z3, z4, cols = 2)
}

Maps

for (i in  1:nrow(school_locations)){
  map(i = i)
}
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.15789,32.796799&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.15789,32.796799&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.15789,32.796799&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.15789,32.796799&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.387941,32.803483&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.387941,32.803483&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.387941,32.803483&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.387941,32.803483&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.059644,32.761032&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.059644,32.761032&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.059644,32.761032&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.059644,32.761032&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.072793,32.727855&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.072793,32.727855&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.072793,32.727855&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.072793,32.727855&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.166508,32.813514&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.166508,32.813514&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.166508,32.813514&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.166508,32.813514&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.925288,32.541663&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.925288,32.541663&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.925288,32.541663&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.925288,32.541663&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.440201,32.777136&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.440201,32.777136&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.440201,32.777136&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.440201,32.777136&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.026829,32.64579&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.026829,32.64579&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.026829,32.64579&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.026829,32.64579&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.096517,32.928233&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.096517,32.928233&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.096517,32.928233&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.096517,32.928233&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.433735,32.779426&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.433735,32.779426&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.433735,32.779426&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.433735,32.779426&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.038168,32.66983&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.038168,32.66983&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.038168,32.66983&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.038168,32.66983&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.452805,32.778036&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.452805,32.778036&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.452805,32.778036&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.452805,32.778036&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.033163,32.633848&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.033163,32.633848&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.033163,32.633848&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.033163,32.633848&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.053757,32.78838&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.053757,32.78838&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.053757,32.78838&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.053757,32.78838&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.8902,32.530505&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.8902,32.530505&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.8902,32.530505&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.8902,32.530505&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.799704,32.856393&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.799704,32.856393&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.799704,32.856393&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.799704,32.856393&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.403867,32.79537&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.403867,32.79537&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.403867,32.79537&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.403867,32.79537&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.863759,32.305601&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.863759,32.305601&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.863759,32.305601&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.863759,32.305601&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.816604,32.896781&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.816604,32.896781&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.816604,32.896781&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.816604,32.896781&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.261006,32.869685&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.261006,32.869685&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.261006,32.869685&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.261006,32.869685&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.898457,32.350019&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.898457,32.350019&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.898457,32.350019&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.898457,32.350019&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.818536,32.515964&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.818536,32.515964&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.818536,32.515964&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-24.818536,32.515964&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false

## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.044112,32.79003&zoom=15&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.044112,32.79003&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.044112,32.79003&zoom=17&size=640x640&scale=2&maptype=satellite&sensor=false
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-25.044112,32.79003&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false